package com.yjz.study.arouter

import android.app.Activity
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.launcher.ARouter

/**
 *作用 :
 *作者 : yjz
 *时间 : 2025/2/14 15:39
 */

fun AppCompatActivity.navigation(path: String) {
    navigation(path, null, -1)
}

fun AppCompatActivity.navigation(path: String, bundle: Bundle? = null) {
    navigation(path, bundle, -1)
}

fun AppCompatActivity.navigation(path: String, requestCode: Int) {
    navigation(path, null, requestCode)
}

fun AppCompatActivity.navigation(path: String, bundle: Bundle? = null, requestCode: Int = -1) {
    val postcard = ARouter.getInstance().build(path).with(bundle)
    if (requestCode == -1) {
        postcard.navigation(this)
    } else {
        postcard.navigation(this, requestCode)
    }
}


fun Fragment.navigation(path: String) {
    navigation(path, null, -1)
}

fun Fragment.navigation(path: String, bundle: Bundle? = null) {
    navigation(path, bundle, -1)
}

fun Fragment.navigation(path: String, bundle: Bundle? = null, requestCode: Int = -1) {
    val postcard: Postcard = ARouter.getInstance().build(path).with(bundle)
    if (requestCode == -1 || context !is Activity) {
        postcard.navigation(context)
    } else {
        postcard.navigation(activity, requestCode)
    }
}